package com.qilu.pe.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Msg;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 消息列表
public class MsgListActivity extends Base2Activity {
    private RecyclerView rcv_list;
    private RelativeLayout vBottomBar;
    private TextView vTagAllRead, vRemove;
    private MsgListAdpter adapter;
    private List<Msg> msgList = new ArrayList<>();
    private boolean isEditMode = false;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_msg_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_list = findViewById(R.id.rcv_list);
        vBottomBar = findViewById(R.id.vBottomBar);
        vTagAllRead = findViewById(R.id.vTagAllRead);
        vTagAllRead.setOnClickListener(this);
        vRemove = findViewById(R.id.vRemove);
        vRemove.setOnClickListener(this);
        setRightText("编辑");
        tv_right_text.setTextColor(Color.parseColor("#316EE5"));
        setHeadTitle("消息列表");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new MsgListAdpter(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        rxList.add(reqDataList());
        showProgress();
    }

    @Override
    protected void onRightTextClick() {
        super.onRightTextClick();
        if (isEditMode) {
            isEditMode = false;
            setRightText("编辑");
            vBottomBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
        } else {
            isEditMode = true;
            setRightText("取消");
            vBottomBar.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.vTagAllRead:
                showProgress();
                APIRetrofit.getDefault().tagAllMsgRead(Global.HEADER)
                        .compose(RxUtil.<BaseResult2>subIoObMain())
                        .subscribe(new Consumer<BaseResult2>() {
                            @Override
                            public void accept(BaseResult2 res) throws Exception {
                                hideProgress();
                                if (res.isSuccess()) {
                                    ToastUtils.showShort("已全部标记为已读");
                                    reqDataList();
                                }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                hideProgress();
                                throwable.printStackTrace();
                            }
                        });
                break;
            case R.id.vRemove:
                ArrayList<String> idsList = new ArrayList<>();
                for (Msg msg : msgList) {
                    if (msg.isChecked()) {
                        idsList.add(msg.getId());
                    }
                }
                if (idsList.isEmpty()) return;
                String[] ids = new String[idsList.size()];
                idsList.toArray(ids);
                final String[] idsArray = ids;
                final NormalDialog dialog = new NormalDialog(this);
                dialog.style(NormalDialog.STYLE_TWO);
                dialog.setTitle("");
                dialog.content("确定要删除选中的消息吗？");
                dialog.setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        showProgress();
                        APIRetrofit.getDefault().delMsg(Global.HEADER, idsArray)
                                .compose(RxUtil.<BaseResult2>subIoObMain())
                                .subscribe(new Consumer<BaseResult2>() {
                                    @Override
                                    public void accept(BaseResult2 res) throws Exception {
                                        hideProgress();
                                        if (res.isSuccess()) {
                                            ToastUtils.showShort("删除成功");
                                            reqDataList();
                                        } else {
                                            ToastUtils.showShort(res.getMsg());
                                        }
                                    }
                                }, new Consumer<Throwable>() {
                                    @Override
                                    public void accept(Throwable throwable) throws Exception {
                                        hideProgress();
                                        throwable.printStackTrace();
                                    }
                                });
                    }
                });
                dialog.show();
                break;
        }
    }

    private Disposable reqDataList() {
        return APIRetrofit.getDefault().reqMsgList(Global.HEADER)
                .compose(RxUtil.<BaseResult2<List<Msg>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Msg>>>() {
                    @Override
                    public void accept(BaseResult2<List<Msg>> baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {
                            msgList = baseResult2.getData();
                            adapter.setDataList(msgList);
                        } else {
                            ToastUtils.showShort(baseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class MsgListAdpter extends ListBaseAdapter<Msg> {

        public MsgListAdpter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_msg_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_status = holder.getView(R.id.tv_status);
            TextView tv_name_code = holder.getView(R.id.tv_name_code);
            ImageView vCheckBox = holder.getView(R.id.vCheckBox);

            Msg item = mDataList.get(position);
            vCheckBox.setTag(item);
            vCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Msg msg = (Msg) v.getTag();
                    msg.setChecked(!msg.isChecked());
                    v.setSelected(msg.isChecked());
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Msg msg = mDataList.get(position);
                    if ("0".equals(msg.getStatus())) {
                        showProgress();
                        APIRetrofit.getDefault().tagMsgRead(Global.HEADER, msg.getId())
                                .compose(RxUtil.<BaseResult2>subIoObMain())
                                .subscribe(new Consumer<BaseResult2>() {
                                    @Override
                                    public void accept(BaseResult2 res) throws Exception {
                                        hideProgress();
                                        if (res.isSuccess()) {
                                            msg.setStatus("1");
                                            notifyDataSetChanged();
                                        }
                                    }
                                }, new Consumer<Throwable>() {
                                    @Override
                                    public void accept(Throwable throwable) throws Exception {
                                        hideProgress();
                                        throwable.printStackTrace();
                                    }
                                });
                    }
                }
            });

            if (isEditMode) {
                vCheckBox.setVisibility(View.VISIBLE);
                vCheckBox.setSelected(item.isChecked());
            } else {
                vCheckBox.setVisibility(View.GONE);
            }

            tv_title.setText(item.getRemark());
            tv_status.setText("0".equals(item.getStatus()) ? "" : "已读");
            tv_name_code.setText(String.format("患者：%1$s  订单单号：%2$s", item.getUsername(), item.getOrder_num()));
        }
    }

    @Override
    public void onBackPressed() {
        setResult(Activity.RESULT_OK);
        super.onBackPressed();
    }
}